

function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return r[2];
    };
    return 0;
 }

chrome.runtime.onMessage.addListener(async (msg, sender) => {
    console.log(" update1688catematch ")
    console.log(msg)
    searchSamePic(msg,false);
});

function searchSamePic(msg,islast){

     var keyword = msg.data.q2;
     console.log("keyword_ "+keyword);
     if(!keyword){
        return;
     }
     $.ajax({
        url: 'https://search.1688.com/service/marketOfferResultViewService?keywords='+keyword+'&spm=a26352.13672862.searchbox.input&beginPage=1&async=true&asyncCount=60&pageSize=60&startIndex=0&pageName=major&&requestId=mD6sGtCDhJxsysdMbhAeZJSWQyWN5yZfGwn1689592639379&sessionId=bbcf0d8ad9fd4845bc4c922a5a71f81d',
    　　　　 type: 'GET',
        xhrFields: {
            withCredentials: true // 这里设置了withCredentials
        },
        success: function(data) {
            var dataObject = JSON.parse(data);
            console.log(dataObject);
            if(dataObject['data']['code']==200 && (!dataObject['data']['data']['offerList'] || dataObject['data']['data']['offerList'].length==0)){
                if(islast==false){
                    console.log("offlist null")
                    msg.data.q2 = msg.data.q3;
                    searchSamePic(msg,true);
                }else{

                    var newData = [];
                    var item = {};
                    item.spu_id = -1;
                    item.cate = -1;
                    newData.push(item);
                    $.post("https://www.gongrenbao.cn/index.php?c=api/ozon/update1688catematch",
                          {dataPro:newData,msg:msg.data},
                          function(data){
                            console.log(data);
                          },
                      "text");
                }
                return;

            }
            var body = dataObject['data']['data']['offerList'];
            var newData = [];
            for (let i = 0; i < body.length; i++) {
               var item={};
               item.spu_id = body[i]['id'];
               item.cate = body[i]['information']['categoryId'];
               newData.push(item);
            }
            $.post("https://www.gongrenbao.cn/index.php?c=api/ozon/update1688catematch",
                  {dataPro:newData,msg:msg.data},
                  function(data){
                    console.log(data);
                  },
              "text");
        },
        error: function(err) {
            console.error(err)
            searchSamePic(page);
        }
    });
}
